﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.IO;

using System.IO.Compression;


using System.Security.Cryptography;

namespace jiemi
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }



        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“dataSet1.HA_IMG”中。您可以根据需要移动或删除它。
            //  this.hA_IMGTableAdapter.Fill(this.dataSet1.HA_IMG);
            // TODO: 这行代码将数据加载到表“dataSet1.HA_COMPBASINFO”中。您可以根据需要移动或删除它。


        }




        private void button4_Click(object sender, EventArgs e)
        {
            openFileDialog2.ShowDialog();
        }



        private void openFileDialog2_FileOk(object sender, CancelEventArgs e)
        {

            if (openFileDialog2.FileNames.Length == 1)
            {


                byte[] bytes = ConvertToBinary(openFileDialog2.FileName.ToString());

                StringBuilder sb = new StringBuilder();

                string aa = "";
                for (int i = 0; i < bytes.Length; i++)
                {
                    aa = Convert.ToString(bytes[i], 16);

                    sb.Append(aa + ",");
                    //if (i % 100 == 0)
                    //{
                    //    sb.Append("\n");
                    //}

                }
                testExecuteUpdate(Compress(sb.ToString()));
                // testExecuteUpdate(sb.ToString());
            }

        }
        public static byte[] ConvertToBinary(string Path)
        {
            FileStream stream = new FileInfo(Path).OpenRead();
            byte[] buffer = new byte[stream.Length];
            stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
            return buffer;
        }


        public static string Compress(string str)
        {
            //因输入的字符串不是Base64所以转换为Base64,因为HTTP如果不传递Base64则会发生http 400错误 
            return Convert.ToBase64String(Compress(Convert.FromBase64String(Convert.ToBase64String(Encoding.Default.GetBytes(str)))));

        }

        public static byte[] Compress(byte[] bytes)
        {

            using (MemoryStream ms = new MemoryStream())
            {
                GZipStream Compress = new GZipStream(ms, CompressionMode.Compress);
                Compress.Write(bytes, 0, bytes.Length);
                Compress.Close();
                return ms.ToArray();
            }

        }






        private void testExecuteUpdate(string a)
        {
            ////文件保存的物理路径，CSTest为虚拟目录名称，F:\Inetpub\wwwroot\CSTest为物理路径
            ////我们在虚拟目录的根目录下建立SchedulerJob文件夹，并设置权限为匿名可修改，
            ////SchedulerJob.txt就是我们所写的文件

            string FILE_NAME = "c:\\file.txt";

            if (!File.Exists(FILE_NAME))
            {
                StreamWriter sr = File.CreateText(FILE_NAME);
                sr.Close();
            }
            else
            {
                File.Delete(FILE_NAME);
                StreamWriter sr = File.CreateText(FILE_NAME);
                sr.Close();
            }
            //向文件写入内容
            StreamWriter x = new StreamWriter(FILE_NAME, true, System.Text.Encoding.Default);
            //if (flag) x.Write("食堂数据更新日志：" + DateTime.Now.ToLongDateString());
            x.Write(a);
            x.Close();
        }



        private void button7_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            StreamReader sr = new StreamReader("c:\\file.txt", Encoding.Default);


            while (sr.Peek() >= 0)
            {
                //sb.Append(sr.ReadLine().Replace("\n",""));
                sb.Append(sr.ReadLine());
            }


            string[] bb = Decompress(sb.ToString()).Split(',');
            //string aaaa = "";
            byte[] bytes1 = new byte[bb.Length - 1];
            for (int i = 0; i < bb.Length - 1; i++)
            {
                bytes1.SetValue(Convert.ToByte(bb[i], 16), i);
            }
            DialogResult btn = saveFileDialog1.ShowDialog();
            if (btn.ToString() == "OK")
            {
                FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.CreateNew);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(bytes1, 0, bytes1.Length);
                bw.Close();
                fs.Close();
            }

            //DataSet1TableAdapters.HA_IMGTableAdapter adapter = new DataSet1TableAdapters.HA_IMGTableAdapter();

            //adapter.InsertQuery("22", bytes1);
        }

        public static string Decompress(string str)
        {
            return Encoding.Default.GetString(Decompress(Convert.FromBase64String(str)));
        }
        public static byte[] Decompress(Byte[] bytes)
        {
            using (MemoryStream tempMs = new MemoryStream())
            {
                using (MemoryStream ms = new MemoryStream(bytes))
                {
                    GZipStream Decompress = new GZipStream(ms, CompressionMode.Decompress);
                    Decompress.CopyTo(tempMs);
                    Decompress.Close();
                    return tempMs.ToArray();

                }

            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SymmetricMethod aa = new SymmetricMethod();
            textBox3.Text = aa.Encrypto(textBox1.Text);


        }

        private void button2_Click(object sender, EventArgs e)
        {
            SymmetricMethod aa = new SymmetricMethod();
            textBox4.Text = aa.Decrypto(textBox3.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox6.Text = Class1.GetMd5Str(textBox5.Text);

        }

        private void button5_Click(object sender, EventArgs e)
        {
            //DataSet1TableAdapters.Z_PROTableAdapter adapter = new DataSet1TableAdapters.Z_PROTableAdapter();

            //adapter.UpdateQuery(textBox6.Text, textBox3.Text);
            //MessageBox.Show("更新成功！");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            button8_Click(null, null);
            saveFileDialog2.FileName = "keygen";
            saveFileDialog2.ShowDialog();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(textBox7.Text) && !String.IsNullOrEmpty(textBox8.Text))
            {
                textBox5.Text = textBox7.Text;

                textBox1.Text = textBox8.Value.ToString("yyyy-MM-dd");

                button1_Click(null, null);

                button3_Click(null, null);


                textBox9.Text = textBox6.Text + "~" + textBox3.Text;
            }
            else
            {
                MessageBox.Show("输入不能为空！");
            }
        }

        private void saveFileDialog2_FileOk(object sender, CancelEventArgs e)
        {

            if (!String.IsNullOrEmpty(textBox9.Text))
            { 
                string FILE_NAME = saveFileDialog2.FileName;

                //取得当前服务器时间，并转换成字符串
                //string c = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + CheckItem + ":" + CheckTime.ToString();
                string temp1 = textBox9.Text;
                //标记是否是新建文件的标量
                bool flag = false;
           
                    StreamWriter sr = File.CreateText(FILE_NAME);
                    sr.Close();
                
                //向文件写入内容
                StreamWriter x = new StreamWriter(FILE_NAME, true, System.Text.Encoding.Default);
                x.Write(temp1);
                x.Close();
                MessageBox.Show("生成成功！");

            }
            else { MessageBox.Show("内容不能为空！"); }
        }






    }
}
